// pages/test/test.js
var QQMapWX = require('../../lib/qqmap-wx-jssdk.js');
var utils = require('../../utils/utils.js')
var content = "";
var length = "";
var longitude;
var latitude;
var name;
var openid="";
var userinfo
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    show:false,
    model: true,
    length: 0,
    scale: 18
    // latitude: 0,
    // longitude: 0,
    // markers: [
    //   {
    //     width: 60, height: 71, iconPath: "/image/deguo.png", id: 0, latitude: 39.906398, longitude: 116.35382, title: ""
    //   }, {
    //     width: 60, height: 71, iconPath: "/image/deguo.png", id: 1, latitude: 39.916398, longitude: 116.361294, title: ""
    //   }, {
    //     width: 60, height: 71, iconPath: "/image/faguo.png", id: 2, latitude: 39.896398, longitude: 116.371294, title: ""
    //   }, {
    //     width: 60, height: 71, iconPath: "/image/yidali.png", id: 3, latitude: 39.886398, longitude: 116.331294, title: ""
    //   }, {
    //     width: 60, height: 71, iconPath: "/image/yinggelan.png", id: 4, latitude: 39.796298, longitude: 116.334294, title: ""
    //   }, {
    //     width: 60, height: 71, iconPath: "/image/bilishi.png", id: 5, latitude: 39.696398, longitude: 116.35382, title: ""
    //   }, {
    //     width: 60, height: 71, iconPath: "/image/baxi.png", id: 6, latitude: 39.926398, longitude: 116.361294, title: ""
    //   }, {
    //     width: 60, height: 71, iconPath: "/image/deguo.png", id: 7, latitude: 39.976298, longitude: 116.371294, title: ""
    //   }]
  },
  // 获取用户信息
  getuser:function (){
    var that=this;
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称.
          wx.getUserInfo({
            success: res => {
              wx.setStorageSync("userinfo", res.userInfo)
              console.log("已经授权")
              // 同时关闭登录界面
              that.setData({show:false})
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        } else {
            // 一键登录
            that.setData({show:true});
            console.log("NO授权")
        }
      }
    })
  },
  // 一建获取授权
  getUserInfo:function(e){ 
    var that =this;
    console.log("请求了这个接口")
    userinfo = JSON.parse(e.detail.rawData);
    wx.setStorageSync("userinfo", userinfo)
    wx.request({
      url: app.globalData.url + "getUserInfo/",
      method: "POST",
      data: {
        openid: openid,
        username: userinfo.nickName,
        sex: userinfo.gender,
        img_url: userinfo.avatarUrl
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res);
        that.setData({show:false})
      },
      fail: function (res) {
      }
    })
  },
  // 传code // 获取openid
  getcode: function (longitude, latitude){
    var that=this;
    wx.login({
      success: function (res) {
        var code = res.code;
        wx.request({
          url: app.globalData.url + "isNewUser/",
          method: "POST",
          data: {
            code:code,
            longitude: longitude,
            latitude: latitude,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            console.log(res);
            wx.setStorageSync('use', res.data)
            openid=res.data.openid;
            that.getuser();
          }, 
          fail: function (res) {
          }
        })

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 0 获取地理坐标
    wx.getLocation({
      type: "gcj02",
      success: function (res) {
        latitude = res.latitude
        longitude = res.longitude
        that.setData({ longitude: longitude, latitude: latitude })
        if (latitude) {
          // 获取openid
          that.getcode(longitude, latitude);
        }
      },
    })
  //  1.获取数据
    this.getInfo();
    // 2.设置地图控件的位置及大小，通过设备宽高定位
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          controls: [{
            id: 1,
            iconPath: '/image/Asset 32.png',
            position: {
              left: 20,
              top: res.windowHeight - 105,
              width: 50,
              height: 50
            },
            clickable: true
          },
          {
            id: 3,
            iconPath: '/image/Asset 42.png',
            position: {
              left: res.windowWidth / 2 - 45,
              top: res.windowHeight - 130,
              width: 90,
              height: 90
            },
            clickable: true
          },
          {
            id: 6,
            iconPath: '/image/Asset 22.png',
            position: {
              left: res.windowWidth - 80,
              top: 10,
              width: 66,
              height: 66
            },
            clickable: true
          },
          {
            id: 5,
            iconPath: '/image/Asset 52.png',
            position: {
              left: res.windowWidth - 70,
              top: res.windowHeight - 105,
              width: 50,
              height: 50
            },
            clickable: true
          },
          {
            id: 4,
            iconPath: '/image/marker.png',
            position: {
              left: res.windowWidth / 2 - 11,
              top: res.windowHeight / 2 - 45,
              width: 22,
              height: 45
            },
            clickable: true
          }]
        })
      }
    });



  },
  // 2.请求服务器，显示附近book，用marker标记
  getInfo: function () {
    var that = this;
    wx.request({
      url: app.globalData.url + 'getAllUsers/',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {},
      success: (res) => {
        var marks = that.addShuXing(res.data.users)
        console.log(marks);
        that.setData({ markers: marks})
      },
      fail: function (res) {

      },
      complete: function (res) {

      }
    })
  },

  //  为返回的对象增加属性
  addShuXing: function (obArray) {
    for (var i = 0; i < obArray.length; i++) {
      obArray[i]['height'] = 60
      obArray[i]['width'] = 50
    }
    return obArray;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */

  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //0 返回首页再次请求数据
    this.getInfo();
    // 1.创建地图上下文，移动当前位置到地图中心
    this.mapCtx = wx.createMapContext("ofoMap");
    this.movetoPosition()
  },
  // 地图控件点击事件
  bindcontroltap: function (e) {
    // 判断点击的是哪个控件 e.controlId代表控件的id，在页面加载时的第3步设置的id
    switch (e.controlId) {
      // 点击定位控件
      case 1: this.movetoPosition();
        break;

      // 点击上传控件，跳转到上传页
      case 3: wx.navigateTo({
        url: '/pages/upload/index'
      });
        break;
      //  点击右上角发完善信息
      case 6: wx.navigateTo({
        url: '/pages/info/info'
      });
        break;
      // 点击头像控件，跳转到个人中心
      case 5: wx.navigateTo({
        url: '/pages/clientMe/clientMe'
      });
        break;
      default: break;
    }
  },
 
  // 地图标记点击事件
  bindmarkertap: function (e) {
    var markerId = e.markerId;
    console.log(markerId);
    wx.navigateTo({
      url: '../marker/marker?markerId=' + markerId,
    })

  },
  // 定位函数，移动位置到地图中心
  movetoPosition: function () {
    this.mapCtx.moveToLocation();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})